#!/usr/bin/env python

import pygame
from pygame.locals import *
from logfile import *
from gfxmanager import *

SCREENSIZE = (800,600)
CAPTION = "Fanning Balls v0.002"
FLAGS = DOUBLEBUF
IMG_BACKGROUND = 'background'
images = [ ('gfx/background.png',IMG_BACKGROUND) ]

class Game:
    def __init__( self ):
        self.run_game = True
        self.log = LogFile( 'game.log' )
        self.log.write( 'Initializing \'Fanning Balls\'' )
        
        self.log.write( 'Initializing screen: ' + str(SCREENSIZE) )
        self.screen = pygame.display.set_mode( SCREENSIZE, FLAGS )
        
        self.log.write( "Set title to {0}".format(CAPTION) )
        pygame.display.set_caption( CAPTION )
        
        self.log.write( 'Creating gfx manager' )
        self.gfx = GfxManager( self.log )
        for image in images:
            self.log.write( 'Loading image ' + image[0] )
            self.gfx.load_image( image[0], image[1] )
        
        return
    
    def handle_events( self ):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.run_game = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    self.run_game = False
        return
    
    def draw( self ):
        self.screen.blit( self.gfx.image(IMG_BACKGROUND), (0,0) )
        pygame.display.flip()
    
    def start( self ):
        self.log.write( "Starting main loop" )
        while self.run_game:
            self.handle_events()
            self.draw()
        return


# Make that this module also can start the game
if __name__ == "__main__":
  game = Game()
  game.start()